//=============================================================================
//
//   File : qtg_config.h
//   Creation date : Sab 19 mar 2011 09:31:42 CET by Antonino G.Imbesi
//
//   This file is part of the QtGLauncher distribution
//   Copyright (C) 2011 Antonino G. Imbesi <grifisx at barmes dot org>
//
//   This program is FREE software. You can redistribute it and/or
//   modify it under the terms of the GNU General Public License
//   as published by the Free Software Foundation; either version 2
//   of the License, or (at your opinion) any later version.
//
//   This program is distributed in the HOPE that it will be USEFUL,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//   See the GNU General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program. If not, write to the Free Software Foundation,
//   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
//=============================================================================

#include <QLabel>
#include <QCheckBox>
#include <QGroupBox>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPushButton>
#include <QLineEdit>
#include <QSpinBox>
#include <QApplication>
#include <QDesktopWidget>
#include <QFile>
#include <QIODevice>
#include <QDebug>
#include <QFileDialog>
#include <QTabWidget>
#include <QSettings>
#include <QDirIterator>
#include <QComboBox>
#include <QXmlStreamReader>

#include "qtg_config.h"
#include "gui_laucher.h"
#include "qtg_listviewitem.h"
#include "qtg_appinstaller.h"
#include "qtg_qpushbutton.h"

qtg_config::qtg_config(QWidget *parent, gui_laucher *origin) :
        QWidget(parent)
{
    m_pOriginWidget = origin;
    realConstructor();
}

qtg_config::qtg_config(QWidget *parent) :
        QWidget(parent)
{
    //m_pOriginWidget = parent;
    realConstructor();
}

void qtg_config::realConstructor()
{
    QWidget *pMainWidget = new QWidget(this);
    pMainWidget->setObjectName("configWidget");

    // Layout della widget figlia di this
    QVBoxLayout *pMainWidgetLayout = new QVBoxLayout;
    pMainWidget->setLayout(pMainWidgetLayout);

    // Layout di this
    QVBoxLayout *pMainVLayout = new QVBoxLayout;
    pMainVLayout->addWidget(pMainWidget);
    setLayout(pMainVLayout);

    QTabWidget *pMainTabWidget = new QTabWidget();
    pMainWidgetLayout->addWidget(pMainTabWidget);

    //---------GroupBox Applicazione:


    QGroupBox *pGbxApp = new QGroupBox(tr("Applicazione"));

    QVBoxLayout *pVLayoutApp= new QVBoxLayout;
    QHBoxLayout *pHLayoutAppHeight = new QHBoxLayout;
    QHBoxLayout *pHLayoutAppWidht = new QHBoxLayout;

    QLabel *pLblHeight = new QLabel(tr("Altezza menu'"));

    m_pSpinBoxHeight = new  QSpinBox;
    m_pSpinBoxHeight->setMaximum(1000);
    m_pSpinBoxHeight->setValue(m_pOriginWidget->m_appConf->value("Config/height").toInt());

    connect(m_pSpinBoxHeight, SIGNAL(valueChanged(int)),
            this, SLOT(setOriginWidgetHeight(int)));

    pHLayoutAppHeight->addWidget(pLblHeight);
    pHLayoutAppHeight->addWidget(m_pSpinBoxHeight);

    QLabel *pLblWidth = new QLabel(tr("Larghezza menu'"));

    m_pSpinBoxWidth = new QSpinBox;
    m_pSpinBoxWidth->setMaximum(1000);
    m_pSpinBoxWidth->setValue(m_pOriginWidget->m_appConf->value("Config/width").toInt());
    connect(m_pSpinBoxWidth, SIGNAL(valueChanged(int)),
            this, SLOT(setOriginWidgetWidth(int)));

    pHLayoutAppWidht->addWidget(pLblWidth);
    pHLayoutAppWidht->addWidget(m_pSpinBoxWidth);


    m_pCkbStyle = new QCheckBox((tr("Menu' senza bordi")));
    if(m_pOriginWidget->m_appConf->value("Config/menuStyle").toBool())
    {
        m_pCkbStyle->setChecked(1);
    }
    connect(m_pCkbStyle, SIGNAL(clicked()),
            this, SLOT(setMenuStyleOff()));

    m_pCkbTranslucent = new QCheckBox(tr("Attiva translucenza"));
    if(m_pOriginWidget->m_appConf->value("Config/translucentWidget").toBool())
    {
        m_pCkbTranslucent->setChecked(1);
    }
    connect(m_pCkbTranslucent, SIGNAL(clicked()),
            this, SLOT(setTranslucentWidgetOff()));

    m_pCkbCloseBtn = new QCheckBox(tr("Abilita pulsante di chiusura"));
    if(m_pOriginWidget->m_appConf->value("Config/closeBtnIsEnabled").toBool())
    {
        m_pCkbCloseBtn->setChecked(1);
    }
    connect(m_pCkbCloseBtn, SIGNAL(clicked()),
            this, SLOT(setCloseBtnIsEnabledOff()));

    pVLayoutApp->addLayout(pHLayoutAppHeight);
    pVLayoutApp->addLayout(pHLayoutAppWidht);
    pVLayoutApp->addWidget(m_pCkbStyle);
    pVLayoutApp->addWidget(m_pCkbTranslucent);
    pVLayoutApp->addWidget(m_pCkbCloseBtn);

    pGbxApp->setLayout(pVLayoutApp);

    pMainTabWidget->addTab(pGbxApp,tr("&Applicazione"));
    //---------GroupBox Recenti:

    QGroupBox *pGbxRecents = new QGroupBox(tr("Menu' recenti"));

    QVBoxLayout *pVLayoutRecents= new QVBoxLayout;
    QHBoxLayout *pHLayoutRecents = new QHBoxLayout;

    m_pCkbRecentsEnable = new QCheckBox(tr("Abilita recenti"));
    pVLayoutRecents->addWidget(m_pCkbRecentsEnable);
    if(m_pOriginWidget->m_appConf->value("Config/recentsIsEnabled").toBool())
    {
        m_pCkbRecentsEnable->setChecked(1);
    }
    connect(m_pCkbRecentsEnable, SIGNAL(clicked()),
            this, SLOT(setRecentsOff()));

    qtg_qpushbutton *pBtnRecentsFile = new qtg_qpushbutton;
    pBtnRecentsFile->setText(tr("Elim&ina Recenti"));
    connect(pBtnRecentsFile, SIGNAL(clicked()),
            this, SLOT(deleteRecents()));
    pBtnRecentsFile->setIcon(QIcon("./themes/"+m_pOriginWidget->m_appConf->value("Config/themePath").toString()+"/clear.png"));

    pHLayoutRecents->addWidget(pBtnRecentsFile);
    pHLayoutRecents->addStretch();

    pVLayoutRecents->addLayout(pHLayoutRecents);

    pGbxRecents->setLayout(pVLayoutRecents);

    pMainTabWidget->addTab(pGbxRecents,tr("&Recenti"));

    //---------GroupBox Themes:

    QGroupBox *pGbxThemes = new QGroupBox(tr("Temi"));

    QVBoxLayout *pVLayoutThemes = new QVBoxLayout;
    QHBoxLayout *pHLayoutThemes = new QHBoxLayout;

    QLabel *pLblThemes = new QLabel(tr("Scegli il tema:"));
    m_pComboThemes = new QComboBox;
    fillThemeSelector();
    m_pComboThemes->setCurrentIndex(m_pComboThemes->findText(m_pOriginWidget->m_appConf->value("Config/themePath").toString()));


    connect(m_pComboThemes, SIGNAL(currentIndexChanged( const QString)),
            this, SLOT(setThemePath(const QString)));

    pHLayoutThemes->addWidget(pLblThemes);
    pHLayoutThemes->addWidget(m_pComboThemes);

    pVLayoutThemes->addLayout(pHLayoutThemes);
    pGbxThemes->setLayout(pVLayoutThemes);

    pMainTabWidget->addTab(pGbxThemes,tr("&Temi"));

    //---------GroupBox Installer Class:

//    qtg_appinstaller *pInstallerClass = new qtg_appinstaller;


    QGroupBox *pGbxInstaller = new QGroupBox(tr("Installa applicazioni"));
    pMainVLayout->addWidget(pGbxInstaller);

    QVBoxLayout *pGbxInstallerVLayout = new QVBoxLayout;
    pGbxInstaller->setLayout(pGbxInstallerVLayout);

    qtg_qpushbutton *pBtnInstaller = new qtg_qpushbutton;
    pBtnInstaller->setText(tr("Installa una nuova applicazione"));
    pBtnInstaller->setIcon(QIcon("./themes/"+m_pOriginWidget->m_appConf->value("Config/themePath").toString()+"/jump.png"));
    pGbxInstallerVLayout->addWidget(pBtnInstaller);
    pMainTabWidget->addTab(pGbxInstaller,tr("&Installer"));

    connect(pBtnInstaller, SIGNAL(clicked()),
            this, SLOT(activateInstallwizard()));
    setMyStyleSheet();
    adjustSize();
    move(QApplication::desktop()->screen()->rect().center() - rect().center());


}

qtg_config::~qtg_config()
{

}

void qtg_config::setMyStyleSheet()
{
    QFile file("./themes/"+m_pOriginWidget->m_appConf->value("Config/themePath").toString()+"/configStyle.qss");
    file.open(QIODevice::ReadWrite);
    QString styleSheet = QLatin1String(file.readAll());
    setStyleSheet(styleSheet);
}

bool qtg_config::deleteRecents()
{
    if(m_pOriginWidget->m_pItemRecents)
    {
        delete m_pOriginWidget->m_pItemRecents;
        // why?????
        m_pOriginWidget->m_pItemRecents=0;
    }
    return QFile::remove("./config/recent");
}

void qtg_config::fillThemeSelector()
{
    QDirIterator it("./themes", QDirIterator::Subdirectories);
    while (it.hasNext()) {
        if(it.fileInfo().isDir() && !it.fileName().contains("."))
            m_pComboThemes->addItem(QIcon("./themes/"+it.fileName()+"/theme.ico"),it.fileName());
        it.next();
    }
}

void qtg_config::setThemePath(const QString theme)
{
    m_pOriginWidget->m_appConf->setValue("Config/themePath",theme);
}

void qtg_config::setRecentsOff()
{
    if(m_pCkbRecentsEnable->isChecked())
        m_pOriginWidget->m_appConf->setValue("Config/recentsIsEnabled",true);
    else
        m_pOriginWidget->m_appConf->setValue("Config/recentsIsEnabled",false);
}

void qtg_config::setMenuStyleOff()
{
    if(m_pCkbStyle->isChecked())
        m_pOriginWidget->m_appConf->setValue("Config/menuStyle",true);
    else
        m_pOriginWidget->m_appConf->setValue("Config/menuStyle",false);
}

void qtg_config::setTranslucentWidgetOff()
{
    if(m_pCkbTranslucent->isChecked())
        m_pOriginWidget->m_appConf->setValue("Config/translucentWidget",true);
    else
        m_pOriginWidget->m_appConf->setValue("Config/translucentWidget",false);
}

void qtg_config::setCloseBtnIsEnabledOff()
{
    if(m_pCkbCloseBtn->isChecked())
        m_pOriginWidget->m_appConf->setValue("Config/closeBtnIsEnabled",true);
    else
        m_pOriginWidget->m_appConf->setValue("Config/closeBtnIsEnabled",false);
}

void qtg_config::setOriginWidgetWidth(int i)
{
    m_pOriginWidget->m_appConf->setValue("Config/width",i);
}

void qtg_config::setOriginWidgetHeight(int i)
{
    m_pOriginWidget->m_appConf->setValue("Config/height",i);
}

void qtg_config::activateInstallwizard()
{
   qtg_appinstaller * pInstallerWizard= new qtg_appinstaller(this, m_pOriginWidget->m_appConf);
    pInstallerWizard->exec();
    delete pInstallerWizard;
}
